package com.wanwu.channel.notify.process;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wanwu.channel.common.enums.ChannelPayStatus;
import com.wanwu.channel.common.enums.ChannelRefundStatus;
import com.wanwu.channel.common.enums.PayType;
import com.wanwu.channel.common.enums.TradeStatusEnum;
import com.wanwu.channel.common.enums.BusinessCodeEnum;
import com.wanwu.channel.common.util.DateUtils;
import com.wanwu.channel.dao.entity.ChannelApplyClearanceEntity;
import com.wanwu.channel.dao.entity.ChannelPayEntity;
import com.wanwu.channel.dao.entity.ChannelRefundEntity;
import com.wanwu.channel.notify.message.MessageNotifyComponent;
import com.wanwu.payment.facade.dto.req.RefundNotifyReqDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

@Component
public class BusinessNotifyProcess {

    private static final Logger logger = LoggerFactory.getLogger(BusinessNotifyProcess.class);

    @Resource
    private MessageNotifyComponent messageNotifyComponent;

    /**
     * 支付回调通知到支付系统
     * @param channelPayEntity
     */
    @Async
    public void notifyToBusinessLine(ChannelPayEntity channelPayEntity) {
        JSONObject notifyReq = new JSONObject();
        notifyReq.put("thirdTradeNo", channelPayEntity.getThirdOrderNo());
        if(PayType.ALLIAN_WECHAT_PAY.value().equals(channelPayEntity.getPayType()) ||
                PayType.ALLIAN_WECHAT_MINI_PAY.value().equals(channelPayEntity.getPayType()) ||
                PayType.ALLIAN_CLEAR_PAY.value().equals(channelPayEntity.getPayType()) ||
                PayType.ALLIAN_ALI_PAY.value().equals(channelPayEntity.getPayType())){
            notifyReq.put("thirdTradeNo", channelPayEntity.getTlOrderNo());
            notifyReq.put("bizCode",channelPayEntity.getBizCode());
        }
        notifyReq.put("channelNo", channelPayEntity.getChannelNo());
        notifyReq.put("payTime", DateUtils.get(1, channelPayEntity.getPayTime()));
        if (ChannelPayStatus.SUCCESS.getValue().equals(channelPayEntity.getPayStatus())) {
            //成功通知
            notifyReq.put("payResult", TradeStatusEnum.SUCCESS.name());
            String fee = JSON.parseObject(channelPayEntity.getNotifyData()).getString("fee");
            notifyReq.put("feeAmount", StringUtils.isEmpty(fee) ? "0" : fee);
        } else if (ChannelPayStatus.CLOSED.getValue().equals(channelPayEntity.getPayStatus())) {
            //关单通知
            notifyReq.put("payResult", ChannelPayStatus.CLOSED.name());
            notifyReq.put("thirdRespCode", channelPayEntity.getErrorCode());
            notifyReq.put("thirdRespMsg", channelPayEntity.getErrorMsg());
        } else {
            //失败通知
            notifyReq.put("payResult", TradeStatusEnum.FAIL.name());
            notifyReq.put("thirdRespCode", channelPayEntity.getErrorCode());
            notifyReq.put("thirdRespMsg", channelPayEntity.getErrorMsg());
        }
        messageNotifyComponent.sendPayNotify(notifyReq);
    }

    /**
     * 渠道回调支付系统报关结果
     * @param dao
     */
    public void notifyToPayment(ChannelApplyClearanceEntity dao){
        //4.回调支付系统20200302新增的
        JSONObject payObj = new JSONObject();
        payObj.put("deliveryNo", dao.getSubOrderNo());
        payObj.put("bizOrderNo", dao.getBizOrderNo());
        payObj.put("applyClearanceResultStatus", dao.getChannelApplyClearanceStatus());
        payObj.put("verDept", dao.getVerDept());
        payObj.put("customsCode", dao.getCustomsCode());
        payObj.put("customsInfo", dao.getCustomsInfo());
        payObj.put("channelNo", dao.getChannelNo());
        payObj.put("bizCode", dao.getBizCode());
        messageNotifyComponent.sendDeclareNotify(payObj);
    }

    /**
     * 退款回调通知到支付系统
     * @param channelRefundEntity
     */
    public void refundNotifyToPayServer(ChannelRefundEntity channelRefundEntity) {
        RefundNotifyReqDto notifyDto = new RefundNotifyReqDto();
        //兼容老业务,未传bizCode时设置默认值wanlimu_code
        notifyDto.setBizCode(Objects.isNull(channelRefundEntity.getBizCode())? BusinessCodeEnum.WANLIMU.getBizCode() : channelRefundEntity.getBizCode());notifyDto.setBizOrderNo(channelRefundEntity.getBizOrderNo());
        notifyDto.setBizRefundNo(channelRefundEntity.getBizRefundNo());
        notifyDto.setChannelNo(channelRefundEntity.getChannelNo());
        notifyDto.setMerchantNo(channelRefundEntity.getMerchantNo());
        notifyDto.setThirdRefundId(channelRefundEntity.getThirdRefundNo());
        notifyDto.setRefundAmount(new BigDecimal(channelRefundEntity.getFinalRefundAmount()));
        notifyDto.setRefundStatus(ChannelRefundStatus.valueOf(channelRefundEntity.getRefundStatus()).name() );
        notifyDto.setSuccessTime(channelRefundEntity.getRefundSuccessTime());
        notifyDto.setErrorCode(channelRefundEntity.getNotifyErrorCode());
        notifyDto.setErrorMsg(channelRefundEntity.getNotifyErrorMsg());
        notifyDto.setQryCode(channelRefundEntity.getQryCode());
        notifyDto.setQryMsg(channelRefundEntity.getQryMsg());
        messageNotifyComponent.sendRefundNotify(notifyDto);
    }
}
